﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Data.Services;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.Profile;
using System.Web.Security;
using VASJ.Model;
using VASJ.Service.Library;
using VASJ.Service.Library.Manager;

namespace VASJ.Service
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public partial class AcpInetService : IAcpInetService
    {
        KeToanDataContext mdb = new KeToanDataContext(ConfigurationManager.ConnectionStrings["KeToanConnectionString"].ConnectionString);

        //[PrincipalPermission(SecurityAction.Demand, Role = "acpinet_service")]
        /// <summary>
        /// Login
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public AcpInetResult Login(string username, string password)
        {
            AcpInetResult r = new AcpInetResult();

            r.Code = -1;
            r.Message = "Login error!";
            r.Token = Guid.Empty;
            r.TokenExpired = DateTime.Now;

            //Check membership
            if (Membership.ValidateUser(username, password))
            {
                //Validation
                MembershipUser mu = Membership.GetUser(username);

                //Return to client
                if (mu != null)
                {
                    r.Code = 0;
                    r.Message = "Login success!";
                }
            }

            return r;
        }

        public AcpInetResult CreateUser(string username, string password)
        {
            AcpInetResult r = new AcpInetResult();

            r.Code = -1;
            r.Message = "Create user error!";
            r.Token = Guid.Empty;
            r.TokenExpired = DateTime.Now;

            //Create user using Membership
            MembershipUser mu = Membership.CreateUser(username, password);

            //Return to client
            if (mu != null)
            {
                r.Code = 0;
                r.Message = "Create user success!";
            }

            return r;
        }

        public AcpInetResult CreateNewUser(string username, string password, string email)
        {
            AcpInetResult r = new AcpInetResult();

            r.Code = -1;
            r.Message = "Create user error!";
            r.Token = Guid.Empty;
            r.TokenExpired = DateTime.Now;

            //Create user using Membership
            MembershipUser mu = Membership.CreateUser(username, password, email);

            //Return to client
            if (mu != null)
            {
                r.Code = 0;
                r.Message = "Create user success!";
            }

            return r;
        }

        public AcpInetResult ChangePassword(string username, string oldpassword, string newpassword)
        {
            AcpInetResult r = new AcpInetResult();

            r.Code = -1;
            r.Message = "Change password error!";
            r.Token = Guid.Empty;
            r.TokenExpired = DateTime.Now;

            //Get existing user
            MembershipUser mu = Membership.GetUser(username);
            
            //Change password
            bool changePwdSuccess = mu.ChangePassword(oldpassword, newpassword);

            //Return to client
            if (changePwdSuccess)
            {
                r.Code = 0;
                r.Message = "Change password success!";
            }

            return r;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "acpinet_service")]
        /// <summary>
        /// Logout
        /// </summary>
        /// <param name="Token"></param>
        /// <returns></returns>
        public AcpInetResult Logout(Guid Token)
        {
            AcpInetResult r = new AcpInetResult();
            r.Code = -1;
            r.Token = Guid.Empty;
            r.TokenExpired = DateTime.Now;

            r.Code = 0;
            r.Message = "Logout success!";

            return r;
        }

        public AcpInetResult GetPermissions(Guid Token, int ProductId, int ProjectId, string PermissionId)
        {
            throw new NotImplementedException();
        }
    }
}
